﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.OleDb;

/// <summary>
///DBAccountStatistics 的摘要说明
/// </summary>
public class DBAccountStatistics
{
	public DBAccountStatistics()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public bool Add(AccountStatistics account)
    {
        return Add(account.AccountName, account.StatusCount, account.RepostCount, account.CommentsCount, account.FollowersCount, account.Date);
    }

    public bool Add(string Account, int StatusCount, int RepostCount, int CommentsCount, int FollowersCount, DateTime Date)
    {

        DbCommand command = DBConfig.CreateCommand("Insert Into [accountStatistics](Account, StatusCount, RepostCount, CommentsCount, FollowersCount,RecordDate) Values(@Account, @StatusCount, @RepostCount, @CommentsCount, @FollowersCount,@RecordDate)");
        command.Parameters.AddRange(
            new DbParameter[]
            {
                DBConfig.CreateParameter("@Account", Account),
                DBConfig.CreateParameter("@StatusCount", StatusCount),
                DBConfig.CreateParameter("@RepostCount", RepostCount),
                DBConfig.CreateParameter("@CommentsCount", CommentsCount),
                DBConfig.CreateParameter("@FollowersCount", FollowersCount),
                DBConfig.CreateParameter("@RecordDate", "#" + Date + "#", OleDbType.DBDate)
            }
            );
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    //根据统计信息ID来删除
    public bool Delete(int id)
    {
        DbCommand command = DBConfig.CreateCommand("Delete FROM [accountStatistics] WHERE ID= @id");
        command.Parameters.Add(DBConfig.CreateParameter("@id", id));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }


    //根据账号日期来删除
    public bool Delete(string account, DateTime date)
    {
        DbCommand command = DBConfig.CreateCommand("Delete FROM [accountStatistics] WHERE RecordDate= @date AND Account=@account");
        command.Parameters.Add(DBConfig.CreateParameter("@RecordDate", "#" + date + "#", OleDbType.DBDate));
        command.Parameters.Add(DBConfig.CreateParameter("@account",  account ));

        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    //删除某一账号下所有数据
    public bool Delete(string account)
    {
        DbCommand command = DBConfig.CreateCommand("Delete FROM [accountStatistics] WHERE Account = @account");
        command.Parameters.Add(DBConfig.CreateParameter("@account", account));
        using (DbConnection conn = DBConfig.CreateConnect())  
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    //删除指定日期之前的所有数据
    public bool DeleteBefore(DateTime date)
    {
        DbCommand command = DBConfig.CreateCommand("Delete FROM [accountStatistics] WHERE RecordDate < @date");
        command.Parameters.Add(DBConfig.CreateParameter("@date", " #" + date + "#", OleDbType.DBDate));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    //返回指定账号所有的统计数据
    public List<AccountStatistics> SelectList(string account)
    {
        if (account == null)
            return null;

        DbCommand command = DBConfig.CreateCommand("Select * From [accountStatistics] WHERE Account = @account");
        command.Parameters.Add(DBConfig.CreateParameter("@account", account));

        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            List<AccountStatistics> result = new List<AccountStatistics>();
            while (reader.Read())
            {
                AccountStatistics data = new AccountStatistics();
                data.AccountName    = reader.GetString(1);
                data.StatusCount    = reader.GetInt32(2);
                data.RepostCount    = reader.GetInt32(3);
                data.CommentsCount  = reader.GetInt32(4);
                data.FollowersCount = reader.GetInt32(5);
                data.Date           = reader.GetDateTime(6);
                result.Add(data);
            }
            return result;
        }
    }

    //返回指定账号某一时间段内的所有数据
    public List<AccountStatistics> SelectList(string account, DateTime begin, DateTime end)
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [accountStatistics] WHERE RecordDate BETWEEN @begin AND @end");
        command.Parameters.Add(DBConfig.CreateParameter("@begin", "#" + begin + "#", OleDbType.DBDate));
        command.Parameters.Add(DBConfig.CreateParameter("@end", "#" + end + "#", OleDbType.DBDate));

        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            List<AccountStatistics> result = new List<AccountStatistics>();
            while (reader.Read())
            {
                AccountStatistics data = new AccountStatistics();
                data.AccountName = reader.GetString(1);
                data.StatusCount = reader.GetInt32(2);
                data.RepostCount = reader.GetInt32(3);
                data.CommentsCount = reader.GetInt32(4);
                data.FollowersCount = reader.GetInt32(5);
                data.Date = reader.GetDateTime(6);
                result.Add(data);
            }
            return result;
        }
    }


}
